#include <iostream>
#include <memory>
#include <string>

#include "HttpServer.hpp"

// 其他方法
void Login(HttpRequest &req, HttpResponse &resp)
{
    // req.Path(): /login
    // 根据req，动态构建username=lisi&password=abcdefg
    LOG(LogLevel::DEBUG) << "进入登录模块" << req.GetPath() << ", " << req.GetArgs();
    std::string req_args = req.GetArgs();
    // 1. 解析参数格式，得到要的参数

    // 2. 访问数据库，验证对应的用户是否是合法的用户，其他工作....
    // TODO
    // SessionManager m;
    // session_id = m.CreateSession(XXXXX);

    // 3. 登录成功
    std::string body = HttpServer::GetContent("wwwlsh/user.html");
    resp.SetStatusCode(200);
    resp.SetHeardKV("Content-Length", std::to_string(body.size()));
    resp.SetHeardKV("Content-Type", "text/html");
    resp.SetBody(body);

    // resp.SetStatusCode(302);
    // resp.SetHeardKV("Location", "/user.html");
}
void Register(HttpRequest &req, HttpResponse &resp)
{
    // 根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入注册模块" << req.GetPath() << ", " << req.GetArgs();
}

void Search(HttpRequest &req, HttpResponse &resp)
{
    // 根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入搜索模块" << req.GetPath() << ", " << req.GetArgs();
}

void Test(HttpRequest &req, HttpResponse &resp)
{
    // 根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入测试模块" << req.GetPath() << ", " << req.GetArgs();
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " <server_ip> <server_port>" << std::endl;
        return 1;
    }
    int port = std::stoi(argv[1]);

    std::unique_ptr<HttpServer> httpser_ptr = std::make_unique<HttpServer>(port);

    // 注册其他请求处理方法
    httpser_ptr->SetRouteMethod("/login", Login);
    httpser_ptr->SetRouteMethod("/register", Register);
    httpser_ptr->SetRouteMethod("/search", Search);
    httpser_ptr->SetRouteMethod("/test", Test);

    // 运行
    httpser_ptr->Start();

    return 0;
}